package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.HisDetailActivity;
import com.xgkj.diyiketang.data.PengYouQuanInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ViewUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrater on 2015/12/28.
 */
public class PengYouQuanAdapter extends BaseAdapter {
    private Context mContext;
    private List<PengYouQuanInfo> mList = new ArrayList<PengYouQuanInfo>();

    public PengYouQuanAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<PengYouQuanInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.pengyouquan_item, null);
            childHolder.mUserImg = (ImageView) convertView.findViewById(R.id.head_icon);
            childHolder.mUserNick = (TextView) convertView.findViewById(R.id.nickname);
            childHolder.mContent = (TextView) convertView.findViewById(R.id.content_desc);
            childHolder.picLayout = (LinearLayout) convertView.findViewById(R.id.pic_list);
            childHolder.picLayout2 = (LinearLayout) convertView.findViewById(R.id.pic_list2);
            childHolder.mTime = (TextView) convertView.findViewById(R.id.dtime);
            childHolder.mZanBtn = (ImageView) convertView.findViewById(R.id.zan);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }


        final PengYouQuanInfo info = mList.get(position);
        if (info != null) {
            String anchorImgUrl = info.getUserLogo();
            if (anchorImgUrl != null && anchorImgUrl.length() > 0) {
                ViewUtil.Size size = ViewUtil.getHeadImageSize_40(mContext);
                childHolder.mUserImg.setTag(anchorImgUrl);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(anchorImgUrl, size));
                if (cacheBitmap != null) {
                    childHolder.mUserImg.setImageBitmap(cacheBitmap);
                } else {
                    childHolder.mUserImg.setImageResource(R.drawable.default_head_image);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.mUserImg, anchorImgUrl, size);
                }
            }
            childHolder.mUserImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String uid = info.getUserId();
                    Intent intent = new Intent(mContext, HisDetailActivity.class);
                    intent.putExtra("USERID", uid);
                    mContext.startActivity(intent);
                }
            });
            if (info.getNickName() != null) {
                try {
                    childHolder.mUserNick.setText(URLDecoder.decode(info.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if (info.getDairyContent() != null) {
                try {
                    childHolder.mContent.setText(URLDecoder.decode(info.getDairyContent(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            childHolder.mTime.setText(info.getDairyTime());
            String isZan = info.getHaveUp();
            if (isZan.equals("1")) {
                childHolder.mZanBtn.setImageResource(R.drawable.zan_checked);
            } else {
                childHolder.mZanBtn.setImageResource(R.drawable.zan_unchecked);
            }

            String ss = info.getDairyImage();
            List<String> apdlist = Arrays.asList(ss.split(";"));
//            PalLog.e("拆分的图片list数量",apdlist.size()+"");
            ArrayList<String> adlist = new ArrayList<String>();
            for (int i = 0; i < apdlist.size(); i++) {
                adlist.add(apdlist.get(i));
            }

            if (ViewUtil.isNull(ss)) {
                childHolder.picLayout.setVisibility(View.GONE);
                childHolder.picLayout2.setVisibility(View.GONE);
            }

            childHolder.picLayout.removeAllViews();
            if (adlist != null && adlist.size() > 0) {

                for (int i = 0; i < adlist.size(); i++) {
                    ImageView image = new ImageView(mContext);
                    image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewUtil.dip2px(mContext, 80), ViewUtil.dip2px(mContext, 80));
                    params.setMargins(0, 10, 20, 10);
                    image.setLayoutParams(params);
                    ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(mContext, 80), ViewUtil.dip2px(mContext, 80));
                    String url = adlist.get(i);
                    if (url != null && url.length() > 0) {
                        ImageCache.getInstance(mContext).loadBitmapToView(image, url, size);
                    }

                    childHolder.picLayout.addView(image);
                    if (i == 2) {
                        break;
                    }
                }
            }
            childHolder.picLayout.requestLayout();

            childHolder.picLayout2.removeAllViews();
            if (adlist != null && adlist.size() > 3) {

                for (int i = 3; i < adlist.size(); i++) {
                    ImageView image = new ImageView(mContext);
                    image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewUtil.dip2px(mContext, 80), ViewUtil.dip2px(mContext, 80));
                    params.setMargins(0, 10, 20, 10);
                    image.setLayoutParams(params);
                    ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(mContext, 80), ViewUtil.dip2px(mContext, 80));
                    String url = adlist.get(i);
                    if (url != null && url.length() > 0) {
                        ImageCache.getInstance(mContext).loadBitmapToView(image, url, size);
                    }

                    childHolder.picLayout2.addView(image);
                    if (i == 5) {
                        break;
                    }
                }
            }
            childHolder.picLayout2.requestLayout();

        }


//        ArrayList<String> adlist = new ArrayList<String>();
//        adlist.add("http://v1.qzone.cc/skin/201310/08/17/18/5253cde1709ed704.jpg!600x600.jpg");
//        adlist.add("http://v1.qzone.cc/skin/201310/08/17/18/5253cdfb94297789.jpg!600x600.jpg");
//        adlist.add("http://v1.qzone.cc/skin/201310/08/17/18/5253ce037d97f728.jpg!600x600.jpg");
//        adlist.add("http://v1.qzone.cc/skin/201310/08/17/16/5253cd697a68b403.jpg!600x600.jpg");
//        adlist.add("http://v1.qzone.cc/skin/201310/08/17/19/5253ce06a29c2425.jpg!600x600.jpg");
//        adlist.add("http://pic49.nipic.com/file/20140928/4949133_154705706000_2.jpg");
//        childHolder.picLayout.removeAllViews();
//        if (adlist != null && adlist.size() > 0) {
//
//            for (int i = 0; i < 3; i++) {
//                ImageView image = new ImageView(mContext);
//                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                params.setMargins(0,10,20,10);
//                image.setLayoutParams(params);
//                ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(mContext, 80), ViewUtil.dip2px(mContext, 80));
//                String url = adlist.get(i);
//                if (url != null && url.length() > 0) {
//                    ImageCache.getInstance(mContext).loadBitmapToView(image, url, size);
//                }
//
//                childHolder.picLayout.addView(image);
//            }
//        }
//        childHolder.picLayout2.removeAllViews();
//        if(adlist!=null && adlist.size()>0){
//
//            for (int i = 3; i < 6; i++) {
//                ImageView image = new ImageView(mContext);
//                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                params.setMargins(0,10,20,10);
//                image.setLayoutParams(params);
//                ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(mContext, 80), ViewUtil.dip2px(mContext, 80));
//                String url = adlist.get(i);
//                if (url != null && url.length() > 0) {
//                    ImageCache.getInstance(mContext).loadBitmapToView(image, url, size);
//                }
//
//                childHolder.picLayout2.addView(image);
//            }
//        }

        return convertView;
    }

    class ChildHolder {
        ImageView mUserImg;
        TextView mUserNick;
        TextView mContent;
        TextView mTime;
        ImageView mZanBtn;
        LinearLayout picLayout;
        LinearLayout picLayout2;
    }
}